package cn.dengta.webapp.user.entity.task;

import java.util.Date;

import cn.dengta.webapp.base.entity.BizEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 任务账户表
 *
 * @author lgy
 * @since 2022-03-05
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("dt_task_account")
public class TaskAccount extends BizEntity {

	/** 用户ID */
	private Long userId;
	/** 总钻石 */
	private Integer totalCoin;
	/** 可用钻石 */
	private Integer activeCoin;
	/** 冻结钻石 */
	private Integer frozenCoin;
	/** 总签到次数 */
	private Integer totalSignCount;
	/** 最近签到时间 */
	private Date lastSignTime;
	/** 连续签到次数 */
	private Integer continuousSignCount;

	public TaskAccount init(Long userId, Date now) {
		this.userId = userId;
		this.totalCoin = 0;
		this.activeCoin = 0;
		this.frozenCoin = 0;
		this.totalSignCount = 0;
		this.lastSignTime = null;
		this.continuousSignCount = 0;
		super.initTime(now);
		super.initActiveStatus();
		return this;
	}

	public void addSignIn(boolean continuous, Date now) {
		this.totalSignCount += 1;
		this.lastSignTime = now;
		this.continuousSignCount = (continuous ? this.continuousSignCount : 0) + 1;
	}

	public Integer opTotalAccount(char operator, Integer coin) {
		return totalCoin = calc(totalCoin, operator, coin);
	}

	public Integer opActiveAccount(char operator, Integer coin) {
		return activeCoin = calc(activeCoin, operator, coin);
	}

	public Integer opFrozenAccount(char operator, Integer coin) {
		return frozenCoin = calc(frozenCoin, operator, coin);
	}

	public static Integer calc(Integer source, char operator, Integer target) {
		switch (operator) {
			case '+':
				return source + target;
			case '-':
				return source - target;
			default:
				return source;
		}
	}

	public boolean hasRemain() {
		return activeCoin > 0;
	}

	public static boolean disabled() {
		return false;
	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public static final String USER_ID = "user_id";
	public static final String TOTAL_COIN = "total_coin";
	public static final String ACTIVE_COIN = "active_coin";
	public static final String FROZEN_COIN = "frozen_coin";
	public static final String TOTAL_SIGN_COUNT = "total_sign_count";
	public static final String LAST_SIGN_TIME = "last_sign_time";
	public static final String CONTINUOUS_SIGN_COUNT = "continuous_sign_count";

}
